package sensor;

import com.hse.network.IEndDevice;
import com.hse.network.Network;
import com.hse.network.NetworkFactory;
import com.hse.network.exceptions.NetworkException;

import java.util.Random;


/**
 * Created by IntelliJ IDEA.
 * User: kate
 * Date: 31.05.2009
 * Time: 17:06:53
 * To change this template use File | Settings | File Templates.
 */
public class TempSensor {

    private IEndDevice endDevice;
    private Network network;
    private final String type = "t";
    private String name = "t1";
    private int temp = 15;


    public TempSensor(String portName, long macAddress) throws InstantiationException {
        //port = portName;
        //macAdd = macAddress;
        endDevice = /*new EndDevice(portName, macAddress);*/NetworkFactory.createEndDevice(portName, macAddress);
    }

    public Network scan() throws NetworkException {
        network = endDevice.scan()[0];
        return network;
    }

    public void connect() throws NetworkException {
        endDevice.connect(network.getPanId());
        endDevice.send((type + ";" + name).getBytes());
        // Connected = true;
    }

    public void send(String data) throws NetworkException {

        endDevice.send(data.getBytes());

    }

    /*public String recv() throws NetworkException{
        return endDevice.recv().toString();
    } */

    public void close() {
        endDevice.close();
        // Connected = false;
    }

// Temperature part

    public void setTemp(int t) {                           // setting up temperature
        this.temp = t;
    }

    public void setTemp(int t, int time, boolean up) {     // t - start temperature, time - time of temperature generation
        Random random = new Random();
        // int i = random.nextInt(4);        // up - true - temperature is gong up, false - goes down
        if (up) {
            t = random.nextInt(time * 2) + t;
        } else t = t - random.nextInt(time * 2);
        this.temp = t;
    }

    public int getTemp() {
        return this.temp;
    }
}
